package com.ruoyi.system.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.vo.PmsPurchaseOrdersVo;
import com.ruoyi.system.domain.bo.PmsPurchaseOrdersBo;
import com.ruoyi.system.service.IPmsPurchaseOrdersService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购订单主，记录采购订单核心信息
 *
 * @author ruoyi
 * @date 2025-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/pmsPurchaseOrders")
public class PmsPurchaseOrdersController extends BaseController {

    private final IPmsPurchaseOrdersService iPmsPurchaseOrdersService;

    /**
     * 查询采购订单主，记录采购订单核心信息列表
     */
    @SaCheckPermission("system:pmsPurchaseOrders:list")
    @GetMapping("/list")
    public TableDataInfo<PmsPurchaseOrdersVo> list(PmsPurchaseOrdersBo bo, PageQuery pageQuery) {
        return iPmsPurchaseOrdersService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购订单主，记录采购订单核心信息列表
     */
    @SaCheckPermission("system:pmsPurchaseOrders:export")
    @Log(title = "采购订单主，记录采购订单核心信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PmsPurchaseOrdersBo bo, HttpServletResponse response) {
        List<PmsPurchaseOrdersVo> list = iPmsPurchaseOrdersService.queryList(bo);
        ExcelUtil.exportExcel(list, "采购订单主，记录采购订单核心信息", PmsPurchaseOrdersVo.class, response);
    }

    /**
     * 获取采购订单主，记录采购订单核心信息详细信息
     *
     * @param poId 主键
     */
    @SaCheckPermission("system:pmsPurchaseOrders:query")
    @GetMapping("/{poId}")
    public R<PmsPurchaseOrdersVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long poId) {
        return R.ok(iPmsPurchaseOrdersService.queryById(poId));
    }

    /**
     * 新增采购订单主，记录采购订单核心信息
     */
    @SaCheckPermission("system:pmsPurchaseOrders:add")
    @Log(title = "采购订单主，记录采购订单核心信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PmsPurchaseOrdersBo bo) {
        return toAjax(iPmsPurchaseOrdersService.insertByBo(bo));
    }

    /**
     * 修改采购订单主，记录采购订单核心信息
     */
    @SaCheckPermission("system:pmsPurchaseOrders:edit")
    @Log(title = "采购订单主，记录采购订单核心信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PmsPurchaseOrdersBo bo) {
        return toAjax(iPmsPurchaseOrdersService.updateByBo(bo));
    }

    /**
     * 删除采购订单主，记录采购订单核心信息
     *
     * @param poIds 主键串
     */
    @SaCheckPermission("system:pmsPurchaseOrders:remove")
    @Log(title = "采购订单主，记录采购订单核心信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{poIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] poIds) {
        return toAjax(iPmsPurchaseOrdersService.deleteWithValidByIds(Arrays.asList(poIds), true));
    }
}
